package com.deanlib.cabinet.ui.recyclerview;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.deanlib.cabinet.base.INetworkAuxiliary;
import com.deanlib.cabinet.data.network.Response;
import com.deanlib.plantern.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public abstract class CommonRecyclerViewAdapter<T> extends
        RecyclerView.Adapter<CommonViewHolder<T>> implements INetworkAuxiliary {

    protected INetworkAuxiliary iNetworkAuxiliary;
    protected int layout;

    protected OnItemClickListener listener;

    public static String TAG = "CommonRecyclerViewAdapter";

    public CommonRecyclerViewAdapter(int layout){
        this.layout = layout;
    }

    /**
     * 要在adapter中使用网络请求的话，请调用这个构造函数
     * @param iNetworkAuxiliary
     */
    public CommonRecyclerViewAdapter(int layout, INetworkAuxiliary iNetworkAuxiliary){
        TAG = getClass().getSimpleName();
        this.layout = layout;
        this.iNetworkAuxiliary = iNetworkAuxiliary;
    }

    /**
     * 这里采用了一种加载布局资源的方式
     * 不同的加载方式对item的显示会有不同的效果
     * 这里使用的是最常用的
     * 如果item的显示不是想要的效果，请重写这个方法
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public CommonViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CommonViewHolder<>(LayoutInflater.from(parent.getContext()).inflate(layout, null));
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder<T> holder, int position) {
        if (listener!=null) {
            holder.itemView.setOnClickListener(view -> {
                listener.onItemClick(view, position);
            });
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next) {
        if (iNetworkAuxiliary!=null){
            iNetworkAuxiliary.request(observable, next);
        }else {
            LogUtils.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error) {
        if (iNetworkAuxiliary!=null){
            iNetworkAuxiliary.request(observable, next, error);
        }else {
            LogUtils.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error, Action complete) {
        if (iNetworkAuxiliary!=null){
            iNetworkAuxiliary.request(observable, next, error, complete);
        }else {
            LogUtils.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public void dispose() {
        if (iNetworkAuxiliary!=null){
            iNetworkAuxiliary.dispose();
        }
    }

    private Map<CommonViewHolder, CompositeDisposable> compositeDisposableMap;

    protected void addDisposable(CommonViewHolder holder, Disposable disposable){
        if (compositeDisposableMap == null){
            synchronized (this){
                if (compositeDisposableMap == null){
                    compositeDisposableMap = new HashMap<>();
                }
            }
        }

        CompositeDisposable compositeDisposable = compositeDisposableMap.get(holder);
        if (compositeDisposable == null){
            synchronized (this) {
                compositeDisposable = compositeDisposableMap.get(holder);
                if (compositeDisposable == null) {
                    compositeDisposable = new CompositeDisposable();
                    compositeDisposableMap.put(holder, compositeDisposable);
                }
            }
        }
        compositeDisposable.add(disposable);

    }

//    @Override
//    public void onViewRecycled(@NonNull CommonViewHolder<T> holder) {
//        super.onViewRecycled(holder);
//        CompositeDisposable compositeDisposable = compositeDisposableMap.get(holder);
//        if (compositeDisposable!=null){
//            compositeDisposable.dispose();
//        }
//    }
//
//    @Override
//    public void onViewDetachedFromWindow(@NonNull CommonViewHolder<T> holder) {
//        super.onViewDetachedFromWindow(holder);
//        CompositeDisposable compositeDisposable = compositeDisposableMap.get(holder);
//        if (compositeDisposable!=null){
//            compositeDisposable.dispose();
//        }
//    }
}
